var express = require('express')
const cors = require('cors')
var app = express()
app.use(cors({ origin: '*' }))
app.use('/', require('./home'))

const axios = require('axios')
const { JSDOM } = require('jsdom')

const refererFn = (config) => {
  return {
    ...config,
    headers: { ...config.headers, Referer: 'https://movie.douban.com' },
  }
}

axios.interceptors.request.use(refererFn)

axios.interceptors.response.use(function (resp) {
  const dom = new JSDOM(resp.data)
  return {
    document: dom.window.document,
    $: dom.window.document.querySelector.bind(dom.window.document),
    $$: function (selector) {
      const $$ = dom.window.document.querySelectorAll.bind(dom.window.document)
      return [...$$(selector)]
    },
    text: resp.data
  }
})

module.exports = app
